/**
 * 
 */
package com.dhcc.cdbp.analyzer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author 谢进军
 *
 */
public class WhiterspaceAnalyzerTest {
	private static final String fieldName = "content";
	
	private static final String value = "Brown The quick fox JUMPED over the lazy dog";
	private static final String queryStr = "Brown";
	Directory directory = null;
	@Before public void setUp() throws Exception {
		directory = new RAMDirectory();
		Analyzer analyzer = new WhitespaceAnalyzer();
		IndexWriterConfig config = new IndexWriterConfig( analyzer);
		IndexWriter writer = new IndexWriter(directory, config);
		Document document = new Document();
		document.add( new TextField(fieldName, value, Store.YES));
		writer.addDocument(document);
		writer.close();
	}
	
	@Test public void test() throws IOException {
		List<Document> result = new ArrayList<>();
		IndexReader reader = DirectoryReader.open(directory);
		IndexSearcher searcher = new IndexSearcher(reader);
		Query query = new TermQuery(new Term(fieldName,queryStr));
		System.out.println(query.toString());
		TopDocs hits = searcher.search(query, 100);
		ScoreDoc[] docs = hits.scoreDocs;
		if (ArrayUtils.isEmpty(docs)) {
			System.out.println("查询不到!!!");
		}else {
			for (ScoreDoc doc : docs) {
				int docId = doc.doc;
				Document document = searcher.doc(docId);
				result.add(document);
			}
		}
		reader.close();
		for( Document document : result){
			System.out.println(document.get(fieldName));
		}
	}
	@Test public void test2() throws IOException, ParseException {
		List<Document> result = new ArrayList<>();
		IndexReader reader = DirectoryReader.open(directory);
		IndexSearcher searcher = new IndexSearcher(reader);
		QueryParser parser = new QueryParser(fieldName,new WhitespaceAnalyzer());
		Query query = parser.parse("Brown t");
		System.out.println(query.toString());
		TopDocs hits = searcher.search(query, 100);
		ScoreDoc[] docs = hits.scoreDocs;
		if (ArrayUtils.isEmpty(docs)) {
			System.out.println("查询不到!!!");
		}else {
			for (ScoreDoc doc : docs) {
				int docId = doc.doc;
				Document document = searcher.doc(docId);
				result.add(document);
			}
		}
		reader.close();
		for( Document document : result){
			System.out.println(document.get(fieldName));
		}
	}
	@After public void tearDown() throws Exception {
		directory.close();
	}
}
